package 并发编程12.CountDownLatch模块;

import lombok.Data;

import java.util.concurrent.CountDownLatch;

/**
 * @author idea
 * @data 2019/1/28
 */
@Data
public class MyThread extends Thread {

    private Service service;
    private final CountDownLatch begin;

    private final CountDownLatch end;


    public MyThread(Service service, CountDownLatch begin, CountDownLatch end) {
        this.service = service;
        this.begin = begin;
        this.end = end;
    }

    @Override
    public void run() {
        try {
            begin.await();  //会堵塞当前线程 直到N变为0,如果该线程一直堵塞，不妨可以尝试一下await中加入相关时间参数来控制
//            begin.await(2,TimeUnit.SECONDS);
            service.testMethod();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        end.countDown();
        System.out.println("end:"+end.getCount());
    }

    public Service getService() {
        return service;
    }

    public MyThread setService(Service service) {
        this.service = service;
        return this;
    }

    public CountDownLatch getBegin() {
        return begin;
    }

    public CountDownLatch getEnd() {
        return end;
    }
}
